package com.ysre.task;

import com.ysre.sheep.Sheep;
import com.ysre.wolf.Wolf;

import java.util.concurrent.Callable;

/**
 * 异步狩猎
 * 具体的执行过程. 此狩猎过程会交给FutureTask.代表告诉每一只狼狩猎的过程都是独立的.
 */
public class CallableHunt implements Callable<HuntResult> {

    private Wolf wolf;

    private Sheep sheep;

    public CallableHunt(Wolf wolf, Sheep sheep){
        this.wolf = wolf;
        this.sheep = sheep;
    }


    @Override
    public HuntResult call() throws Exception {
        boolean b = wolf.attack(sheep);
        Thread.sleep((long) (500 + Math.random() * 500));
        String result = null;
        if(b){
            result = wolf.getName() + "吃掉了" + sheep.getName();
        }else{
            result = wolf.getName() + "被" + sheep.getName() +"干掉了";
        }
        HuntResult huntResult = new HuntResult(wolf,sheep,b,result);
        return huntResult;
    }
}
